package com.codi.api.validation;

import com.codi.base.cache.CacheUtil;
import com.codi.base.util.MoneyUtils;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.annotation.Validator;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.UserService;
import com.codi.bus.core.vo.OrganBuyVo;
import com.codi.bus.core.vo.OrganSellVo;
import com.codi.bus.core.vo.OrganUndoVo;
import com.codi.message.constant.ErrorConst;
import com.codi.message.constant.ErrorConstant;
import com.codi.message.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 机构相关校验
 *
 * @author shi.pengyan
 * @version 1.0 2017-09-12 15:07
 * @since 1.0
 */
@Slf4j
@Validator
@SuppressWarnings("Duplicates")
public class OrganTradeValidation extends TradeValidation {

    @Autowired
    private UserService userService;

    /**
     * 验证申购输入参数
     *
     * @param requestVo
     * @param bankAccount
     * @param paymentType
     * @param userId
     * @return
     */
    public BaseResult validateOrganBuy(OrganBuyVo requestVo, String bankAccount, String paymentType, long userId) {

        BaseResult result = null;

        result = baseValidate(bankAccount, paymentType, requestVo.getFundName(), requestVo.getFundCode(),
            requestVo.getTradePassword());

        if (result != null) {
            return result;
        }

        // 如果两个数一样则返回0，如果第一个数比第二个数大则返回1，反之返回-1
        if (MoneyUtils.compareTo(requestVo.getFundBalance(), 0) != 1) {
            result = new BaseResult();
            result.setErrorCode(ErrorConstant.ERROR_FIELD_NO_FIND);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_FIELD_NO_FIND,
                ErrorConstant.PLACEHOLDER_FIELD_NO_FIND_FIELD, "购买金额"));
            return result;
        }


        // 验证是否重复提交, 5分钟过期
        String applyNo = requestVo.getApplyNo() + userId;

        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_BUY, applyNo);
        if (!StringUtil.isEmpty(applyCache.get(applyKey))) {
            result = new BaseResult();
            result.setSuccess(false);
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_DUPLICATE_APPLY, null));

            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        Boolean orgAcctIsOpen = userService.checkOrgAcctIsOpen(userId);
        if (!orgAcctIsOpen) {
            result = new BaseResult(false);
            result.setErrorCode(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC));
            return result;
        }

        return null;
    }


    /**
     * 验证赎回输入参数
     *
     * @param requestVo
     * @param userId
     * @return
     */
    public BaseResult validateOrganSell(OrganSellVo requestVo, long userId,long profileId) {
        BaseResult result = null;

        result = baseValidate(requestVo.getBankAccount(), requestVo.getPaymentType(), requestVo.getFundName(),
            requestVo.getFundCode(), requestVo.getTradePassword());

        if (result != null) {
            return result;
        }

        // 如果两个数一样则返回0，如果第一个数比第二个数大则返回1，反之返回-1
        if (MoneyUtils.compareTo(requestVo.getShares(), 0) != 1) {
            result = new BaseResult();
            result.setSuccess(false);
            result.setErrorCode(ErrorConstant.ERROR_FIELD_NO_FIND);
            Map<String, String> map = new HashMap<>();
            map.put(ErrorConstant.PLACEHOLDER_FIELD_NO_FIND_FIELD, "赎回份额");
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_FIELD_NO_FIND, map));
            return result;
        }

        // 验证是否重复提交, 5分钟过期
        String applyNo  = requestVo.getApplyNo() + userId;
        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_SELL, applyNo);
        if (!StringUtil.isEmpty(applyCache.get(applyKey))) {
            result = new BaseResult();
            result.setSuccess(false);
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_DUPLICATE_APPLY, null));
            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        Boolean orgAcctIsOpen = userService.checkOrgAcctStatus(profileId);
        if (!orgAcctIsOpen) {
            result = new BaseResult(false);
            result.setErrorCode(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC));
            return result;
        }

        return null;
    }


    /**
     * 验证撤单参数
     *
     * @param requestVo
     * @param userId
     * @return
     */
    public BaseResult validateUndo(OrganUndoVo requestVo, Long userId) {

        BaseResult result = new BaseResult();
        result.setSuccess(false);


        // 验证是否重复提交, 5分钟过期
        String applyNo  = requestVo.getApplyNo() + userId;
        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_UNDO, applyNo);
        if (!StringUtil.isEmpty(applyCache.get(applyKey))) {
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_DUPLICATE_APPLY, null));

            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        Boolean orgAcctIsOpen = userService.checkOrgAcctIsOpen(userId);
        if (!orgAcctIsOpen) {
            result = new BaseResult(false);
            result.setErrorCode(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.ORGAN_OPEN_ACCT_IS_NOT_SUC));
            return result;
        }

        return null;
    }


}
